<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class brand_model extends model {
	public function __construct() {
		$this->db_config = pc_base::load_config('database');
		$this->db_setting = 'default';
		$this->table_name = 'brand';
		parent::__construct();
	}
	
	/**
	 * 更新关联产品
	 * @param $bid 品牌ID
	 * @param $catid  更新后的栏目id
	 */
	public function updateRelativeProduct($bid, $catid)
	{
		$brand = $this->get_one(array('id'=>$bid));
		$bids = $brand['id'];
		if ($brand['parentid']==0) {
			$brand_child = $this->select(array('parentid'=>$brand['id']), 'id,name,parentid');
			if ($brand_child) {
				foreach ($brand_child AS $v) {
					$bids .= ','.$v['id'];
				}
			}
		}
		
		$content_db = pc_base::load_model('content_model');
		$content_db->set_model(11);
		$attachment_db = pc_base::load_model('attachment_model');
		$attachment_index_db = pc_base::load_model('attachment_index_model');
		$hits_db = pc_base::load_model('hits_model');
		$tags_content_db = pc_base::load_model('tags_content_model');
		
		$content_list = $content_db->select('`bids` IN('.$bids.')', 'id,catid');
		foreach ($content_list AS $c) {
			$content_db->update(array('catid'=>$catid), '`id`='.$c['id']);
			$hits_db->update(array('catid'=>$catid), 'hitsid="c-11-'.$c['id'].'"');
			$tags_content_db->update(array('catid'=>$catid, 'contentid='.$c['id']));
			
			//附件关联表
			$att = $attachment_index_db->select(array('keyid'=>'c-'.$c['catid'].'-'.$c['id']), 'keyid,aid');
			if ($att) {
				foreach ($att AS $at) {
					$attachment_index_db->update(array('keyid'=>'c-'.$catid.'-'.$c['id']), 'keyid="'.$at['keyid'].'"');
					$attachment_db->update(array('catid'=>$catid), '`aid`='.$at['aid']);
				}
			}
		}
	}
}
?>